package com.help10000.rms.ui.activitys

import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.InputMethodManager
import androidx.core.widget.addTextChangedListener
import com.amir.common.beans.GeoCoderData
import com.amir.common.beans.MapNavigationData
import com.amir.common.beans.TencentMapSearchData
import com.amir.common.utils.IMEUtil
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.NavigationPresenter
import com.help10000.rms.mvp.view.NavigationView
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.windows.SearchKeyPopuWindow
import com.help10000.rms.ui.windows.SelectNavigationMapDialog
import com.tencent.tencentmap.mapsdk.maps.model.LatLng
import kotlinx.android.synthetic.main.activity_navigation.*


/**
 * 地图
 */
class MapNavigationActivity : BaseActivity<NavigationPresenter, NavigationView>(), NavigationView {

    var manager: InputMethodManager? = null
    override fun getlayoutId(): Int {
        return R.layout.activity_navigation
    }

    override fun initView() {
        manager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        mPresenter.mTencentMap = map.map


    }

    override fun onStart() {
        super.onStart()
        map.onStart()
    }

    override fun onResume() {
        super.onResume()
        map.onResume()

    }

    var address: String? = null
    var navigationData: MapNavigationData? = null;
    var fromUser: Boolean = true
    val popuList = mutableListOf<TencentMapSearchData>()

    // var searchData: TencentMapSearchData? = null
    override fun initData(savedInstanceState: Bundle?) {
        searchKeyPopuWindow = SearchKeyPopuWindow(mContext, R.layout.popu_search_map, popuList)
        searchKeyPopuWindow?.setSearchSelectResultListener(object :
            SearchKeyPopuWindow.SearchSelectResultListener {
            override fun onResult(searchData: TencentMapSearchData?) {
                //this@MapNavigationActivity.searchData = searchData

                navigationData = MapNavigationData()
                navigationData?.title = searchData?.title
                navigationData?.address = searchData?.address
                navigationData?.location = searchData?.location

                val latLng = LatLng()
                latLng.latitude = searchData?.location?.lat!!
                latLng.longitude = searchData.location.lng
                fromUser = true
                et_address.setText(searchData.title)
                et_address.setSelection(searchData.title.length)
                mPresenter.showMarker(latLng)
                searchKeyPopuWindow?.dismiss()
                IMEUtil.hideIME(mContext, et_address)
            }
        })
        address = intent.getStringExtra("data")
        if (!address.isNullOrEmpty()) {
            et_address.setText(address)
            mPresenter.geocoderFromAddress(address!!)
        } else {
            val location = intent.getStringExtra("location")
            mPresenter.geocoderFromLocation(location)
        }
    }

    override fun initListener() {
        super.initListener()
        btn_search.setOnClickListener(DuplicateClickProxy {
            val text = et_address.text.toString()
            if (text.isEmpty()) {
                ToastUtil.show("请输入搜索内容")
                return@DuplicateClickProxy
            }
            mPresenter.searchKey(text)
        })
        et_address.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_ENTER && event?.getAction() == KeyEvent.ACTION_DOWN) {
                //先隐藏键盘
                if (manager?.isActive == true) {
                    manager?.hideSoftInputFromWindow(et_address.applicationWindowToken, 0);
                }
                if (et_address.text.toString().trim().isNotBlank()) {
                    mPresenter.searchKey(et_address.text.toString().trim())
                }

            }

            false
        }
        et_address.addTextChangedListener {
            if (fromUser) {
                fromUser = false
                return@addTextChangedListener
            }
            if (it.toString().trim().isNotBlank()) {
                //  et_address.requestFocus()
                // mPresenter.searchKey(it.toString().trim())
            }
        }

        iv_daohang.setOnClickListener(DuplicateClickProxy {
            if (navigationData == null) {
                ToastUtil.show("请输入要去的地点")
                return@DuplicateClickProxy
            }
            val selectNavigationMapDialog =
                SelectNavigationMapDialog(
                    mContext,
                    navigationData!!,
                    mPresenter.tencentLocation
                )
            selectNavigationMapDialog.show()
        })
    }

    override fun showLoading() {

    }

    var searchKeyPopuWindow: SearchKeyPopuWindow? = null

    override fun searchSuccess(data: List<TencentMapSearchData>, type: Int) {
        try {
            if (searchKeyPopuWindow != null) {
                if (searchKeyPopuWindow?.isShowing == false) {
                    searchKeyPopuWindow?.showPopu(data, et_address)
                } else {
                    searchKeyPopuWindow?.notifyDataSetChanged(data)
                }
            }

        } catch (e: Exception) {

        }

    }

    override fun geocoderSuccess(data: GeoCoderData?) {
        if (data == null) {
            mPresenter.showLocation()
            return
        }
        et_address.setText(if (data.address.isNullOrEmpty()) address else data.address)
        navigationData = MapNavigationData()
        navigationData?.title = if (data.title.isNullOrEmpty()) data.address else data.title
        navigationData?.address = if (data.address.isNullOrEmpty()) address else data.address
        navigationData?.location = data.location

        val latLng = LatLng()
        latLng.latitude = data?.location?.lat
        latLng.longitude = data?.location?.lng
        mPresenter.showMarker(latLng)
    }

    override fun createPresenter(): NavigationPresenter {
        return NavigationPresenter(this)
    }


    override fun onPause() {
        super.onPause()
        map.onPause()
    }

    override fun onStop() {
        super.onStop()
        map.onStop()
    }

    override fun onRestart() {
        super.onRestart()
        map.onRestart()
    }

    override fun onDestroy() {
        map.onDestroy()
        super.onDestroy()

    }


}
